﻿function Load() {
    this.load = 0;
    this.cgArray = new Array();
    this.peopleArray = new Array();
    this.peopleHeadArray = new Array();
    this.bgmArray = new Array();
    this.voiceArray = new Array();
    this.loading = false;
    this.objGuid = "{55BC4851-CE8C-4210-93C7-2EFF1E9A520F},";
    this.objId = 0;
    //
    this.cgLoaded = 0;
    this.peopleLoaded = 0;
    this.peopleHeadLoaded = 0;
    this.bgmLoaded = 0;
    this.voiceLoaded = 0;
    //
    this.xmlObjectArray = new Array();
    this.loadSuccess = true;
}
Load.prototype.ReadXml = function (arg) {
    //var __this = this;
    $.ajax({
        type: "POST",
        url: "Load.aspx/ReadScriptXml",
        contentType: "application/json; charset=utf-8",
        dataType: "xml",
        data: "{load:" + config.load.toString() + ",nowload:" + this.load.toString() + "}",
        context: this,
        success: function (loadxml) {
            this.load += config.load;
            GameMain.logManager.add(0);
            arg.callback.call(arg.context, loadxml, this);
        }, error: arg.errcallback
    });

}
Load.prototype.LoadXmlObjectToArray = function (xml) {
    var objNode = null;
    //CG
    var xmlcgArray = this.xmlObjectArray[0] || new Array();
    objNode = xml.getElementsByTagName("CG");
    for (var i = 0; i < objNode.length; i++) { xmlcgArray.push(objNode[i]); }
    this.xmlObjectArray[0] = xmlcgArray;

    //Bgm
    var xmlbgmArray = this.xmlObjectArray[1] || new Array();
    objNode = xml.getElementsByTagName("Bgm");
    for (var i = 0; i < objNode.length; i++) { xmlbgmArray.push(objNode[i]); }
    this.xmlObjectArray[1] = xmlbgmArray;

    //People
    var xmlpeopleArray = this.xmlObjectArray[2] || new Array();
    objNode = xml.getElementsByTagName("People");
    for (var i = 0; i < objNode.length; i++) { xmlpeopleArray.push(objNode[i]); }
    this.xmlObjectArray[2] = xmlpeopleArray;

    //PeopleHead
    var xmltextArray = this.xmlObjectArray[3] || new Array();
    objNode = xml.getElementsByTagName("Ph");
    for (var i = 0; i < objNode.length; i++) { xmltextArray.push(objNode[i]); }
    this.xmlObjectArray[3] = xmltextArray;

    //Voice
    var xmltextArray = this.xmlObjectArray[4] || new Array();
    objNode = xml.getElementsByTagName("Label");
    for (var i = 0; i < objNode.length; i++) { xmltextArray.push(objNode[i]); }
    this.xmlObjectArray[4] = xmltextArray;
}
Load.prototype.Preload = function (callback) {
    var _this = this;
    var xmlObjectArray = null;
    //CG
    xmlObjectArray = this.xmlObjectArray[0];
    this.LoadCG(xmlObjectArray);

    //BGM
    xmlObjectArray = this.xmlObjectArray[1];
    this.LoadBgm(xmlObjectArray);

    //People
    xmlObjectArray = this.xmlObjectArray[2];
    this.LoadPeople(xmlObjectArray);

    //PeopleHead
    xmlObjectArray = this.xmlObjectArray[3];
    this.LoadPeopleHead(xmlObjectArray);

    //Voice
    xmlObjectArray = this.xmlObjectArray[4];
    this.LoadVoice(xmlObjectArray);


    //    if ((this.cgLoaded < config.cg / 2 && !this.cgEnd) ||
    //        (this.peopleLoaded < config.people / 2 && !this.peopleEnd) ||
    //        (this.bgmLoaded < config.bgm / 2 && !this.bgmEnd) ||
    //        (this.voiceLoaded < config.voice / 2 && !this.voiceEnd)) {
    //        GameMain.contentWindow.logManager.add(1);
    //    }
    if (!this.LoadPoll()) {
        //mask
        GameMain.ShowMask(true);
        var max = this.getDifference();
        var _int = window.setInterval(function () {
            if (_this.LoadPoll()) {
                window.clearInterval(_int);
                if (GameMain.HideMask(true)) {
                    callback();
                }
            }
            else {
                var now = _this.getDifference();
                GameMain.drawProgress(max, max - now);
            }
        }, config.loadStateLoopTime);
    }
    else
        callback();
}
Load.prototype.LoadPoll = function () {
    if (!this.loading) {
        if ((this.cgLoaded == 0 && this.cgArray.length != 0) ||
   (this.peopleLoaded == 0 && this.peopleArray.length != 0) ||
   (this.peopleHeadLoaded == 0 && this.peopleHeadArray.length != 0) ||
   (this.bgmLoaded == 0 && this.bgmArray.length != 0) ||
   (this.voiceLoaded == 0 && this.voiceArray.length != 0) || !config.imageLoaded) {
            this.loading = true;
        }
    }
    if (this.loading) {
        if (((this.cgArray.length >= config.minresources && this.cgLoaded >= config.minresources) ||
         (this.cgArray.length < config.minresources && this.cgLoaded == this.cgArray.length))
          &&
          ((this.peopleArray.length >= config.minresources && this.peopleLoaded >= config.minresources) ||
         (this.peopleArray.length < config.minresources && this.peopleLoaded == this.peopleArray.length))
         &&
        ((this.peopleHeadArray.length >= config.minresources && this.peopleHeadLoaded >= config.minresources) ||
         (this.peopleHeadArray.length < config.minresources && this.peopleHeadLoaded == this.peopleHeadArray.length))
         &&
         ((this.bgmArray.length >= config.minresources && this.bgmLoaded >= config.minresources) ||
         (this.bgmArray.length < config.minresources && this.bgmLoaded == this.bgmArray.length))
         &&
         ((this.voiceArray.length >= config.minresources && this.voiceLoaded >= config.minresources) ||
         (this.voiceArray.length < config.minresources && this.voiceLoaded == this.voiceArray.length))
                     ) {
            this.loading = false;
            return true;
        }
        else
            return false;
    }
    else
        return true;
}
Load.prototype.LoadCG = (function () {
    var cgi = 0;
    var _this = null;
    var onLoad = function () {
        _this.cgLoaded++;
        GameMain.SystemInfo(_this);
    }
    var onError = function () {
        GameMain.logManager.add(11, this, onReload, "&cgUrl=" + this.src);
        this.true_src = this.src;
        this.src = config.errorImage.src;
        _this.loadSuccess = false;
    }
    var onReload = function () {
        _this.cgLoaded--;
        GameMain.SystemInfo(_this);
        this.src = this.true_src + "?";
    }
    return function (loadList) {
        _this = this;
        for (cgi; cgi < loadList.length && _this.cgArray.length < config.cg; cgi++) {
            var node = loadList[cgi];
            var src = node.getAttribute("Src");
            if (src != null && src != "") {
                var cg = Tool.Image.Load("Images/CG/" + src, onLoad, onError);
                cg.id = _this.objGuid + (_this.objId++).toString();
                _this.cgArray.unshift(cg);
            }
        }
    }
})()
Load.prototype.LoadPeople = (function () {
    var peoplei = 0;
    var _this = null;
    var onLoad = function () {
        _this.peopleLoaded++;
        GameMain.SystemInfo(_this);
    }
    var onError = function () {
        GameMain.logManager.add(12, this, onReload, "&peopleUrl=" + this.src);
        this.src = config.errorImage.src;
        _this.loadSuccess = false;
    }
    var onReload = function (src) {
        _this.peopleLoaded--
        GameMain.SystemInfo(_this);
        this.src = src + "?";
    }
    return function (loadList) {
        _this = this;
        for (peoplei; peoplei < loadList.length && _this.peopleArray.length < config.people; peoplei++) {
            var load = loadList[peoplei];
            if (load.getAttribute("Src") != null && load.getAttribute("Src") != "") {
                var people = new Image();
                people.src = "Images/People/" + load.getAttribute("Src");
                people.onload = onLoad;
                people.onerror = onError;
                _this.peopleArray.unshift(people);
            }
        }
    }
})()
Load.prototype.LoadPeopleHead = (function () {
    var peopleHeadi = 0;
    var _this = null;
    var onLoad = function () {
        _this.peopleHeadLoaded++;
        GameMain.SystemInfo(_this);
    }
    var onError = function () {
        GameMain.logManager.add(15, this, onReload, "&peopleHeadUrl=" + this.src);
        this.true_src = this.src;
        this.src = config.error_peopleHead_image.src;
        _this.loadSuccess = false;
    }
    var onReload = function () {
        _this.peopleHeadLoaded--;
        GameMain.SystemInfo(_this);
        this.src = this.true_src + "?";
    }
    return function (loadList) {
        _this = this;
        for (peopleHeadi; peopleHeadi < loadList.length && _this.peopleHeadArray.length < config.peopleHead; peopleHeadi++) {
            var node = loadList[peopleHeadi];
            var src = node.getAttribute("Src");
            if (src != null && src != "") {
                var ph = Tool.Image.Load("Images/peopleHead/" + src, onLoad, onError);
                ph.id = _this.objGuid + (_this.objId++).toString();
                _this.peopleHeadArray.unshift(ph);
            }
        }
    }
})()
Load.prototype.LoadBgm = (function () {
    var bgmi = 0;
    var _this = null;
    var canplaythrough = function () {
        _this.bgmLoaded++;
        GameMain.SystemInfo(_this);
    }
    var error = function () {
        _this.bgmLoaded++;
        _this.loadSuccess = false;
        GameMain.SystemInfo(_this);
        GameMain.logManager.add(13, this, onReload, "&BgmUrl=" + this.src);
    }
    var onReload = function () {
        _this.bgmLoaded--
        GameMain.SystemInfo(_this);
        this.src = this.src + "?";
        this.load();
    }
    var ended = function () {
        this.removeEventListener('canplaythrough', canplaythrough, false);
        this.play();
    }
    return function (loadList) {
        _this = this;
        for (bgmi; bgmi < loadList.length && _this.bgmArray.length < config.bgm; bgmi++) {
            var node = loadList[bgmi];
            var src = node.getAttribute("Src");
            if (src != null && src != "") {
                var audio = Tool.audioCreate("Bgm/" + config.audioType + "/" + src + "." + config.audioType, canplaythrough, error, ended, true);
                //var audio = Tool.audioCreate("Bgm/" + config.audioType + "/" + src + ".mp3", canplaythrough, error, ended, true);
                audio.id = _this.objGuid + (_this.objId++).toString();
                _this.bgmArray.unshift(audio);
                audio.load();
            }
        }
    }
})()
Load.prototype.LoadVoice = (function () {
    var voicei = 0;
    var _this = null;
    var canplaythrough = function () {
        _this.voiceLoaded++;
        GameMain.SystemInfo(_this);
    }
    var error = function () {
        _this.voiceLoaded++;
        GameMain.SystemInfo(_this);
        GameMain.logManager.add(14, this, onReload, "&VoiceUrl=" + this.src);
        _this.loadSuccess = false;
    }
    var onReload = function () {
        _this.voiceLoaded--
        GameMain.SystemInfo(_this);
        this.src += "?";
        this.load();
    }
    return function (textList) {
        _this = this;
        for (voicei; voicei < textList.length && _this.voiceArray.length < config.voice; voicei++) {
            var node = textList[voicei];
            var src = node.getAttribute("Voice");
            if (src != null && src != "") {
                var audio = Tool.audioCreate("Voice/" + config.audioType + "/" + src + "." + config.audioType, canplaythrough, error, null, false);
                //var audio = Tool.audioCreate("Voice/" + config.audioType + "/" + src + ".mp3", canplaythrough, error, null, false);
                audio.id = _this.objGuid + (_this.objId++).toString();
                _this.voiceArray.unshift(audio);
                audio.load();
            }
        }
    }
})()
Load.prototype.getDifference = function () {
    var q = 0;
    if (this.cgArray.length != 0)
        q = q + (config.minresources - this.cgLoaded > 0 ? config.minresources - this.cgLoaded : 0);
    if (this.peopleArray.length != 0)
        q = q + (config.minresources - this.peopleLoaded > 0 ? config.minresources - this.peopleLoaded : 0);
    if (this.peopleHeadArray.length != 0)
        q = q + (config.minresources - this.peopleHeadLoaded > 0 ? config.minresources - this.peopleHeadLoaded : 0);
    if (this.bgmArray.length != 0)
        q = q + (config.minresources - this.bgmLoaded > 0 ? config.minresources - this.bgmLoaded : 0);
    if (this.voiceArray.length != 0)
        q = q + (config.minresources - this.voiceLoaded > 0 ? config.minresources - this.voiceLoaded : 0);
    return q;
}